import time
import unittest

from Util.LoginUtil import login_util


class Personcenter(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = login_util('chrome')
        cls.driver.request('http://39.101.167.251/xinhu/')
        time.sleep(3)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.qt()

    # 测试修改密码
    # 旧密码不正确
    def test_01(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[1]/div/input', 'admin')
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', 'qf123456')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        self.driver.click('xpath', '//*[@id="menulist"]/a[1]')
        self.driver.click('xpath', '//*[@id="menu_list_grcog"]')
        time.sleep(3)
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf1234')
        self.driver.send_keys('name', 'passwordPost', 'qf123456')
        self.driver.send_keys('name', 'password1Post', 'qf123456')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:red"]').text
        self.assertEqual('旧密码不正确', pw, msg='预期值与实际值不符')
        time.sleep(3)

    # 密码不含有数字
    def test_02(self):
        self.driver.upclear('name', 'passoldPost')
        self.driver.upclear('name', 'passwordPost')
        self.driver.upclear('name', 'password1Post')
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123456')
        self.driver.send_keys('name', 'passwordPost', 'admin')
        self.driver.send_keys('name', 'password1Post', 'admin')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:red"]').text
        self.assertEqual('新密码必须使用字母+数字', pw, msg='预期值与实际值不符')
        time.sleep(3)

    # 密码中不含有字母
    def test_03(self):
        self.driver.upclear('name', 'passoldPost')
        self.driver.upclear('name', 'passwordPost')
        self.driver.upclear('name', 'password1Post')
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123456')
        self.driver.send_keys('name', 'passwordPost', '123456')
        self.driver.send_keys('name', 'password1Post', '123456')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:red"]').text
        self.assertEqual('新密码必须使用字母+数字', pw, msg='预期值与实际值不符')
        time.sleep(3)

    # 前后密码不一致
    def test_04(self):
        self.driver.upclear('name', 'passoldPost')
        self.driver.upclear('name', 'passwordPost')
        self.driver.upclear('name', 'password1Post')
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123456')
        self.driver.send_keys('name', 'passwordPost', 'qf12345')
        self.driver.send_keys('name', 'password1Post', 'qf213')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:red"]').text
        self.assertEqual('确认密码不一致', pw, msg='预期值与实际值不符')
        time.sleep(3)

    # 密码少于四位
    def test_05(self):
        self.driver.upclear('name', 'passoldPost')
        self.driver.upclear('name', 'passwordPost')
        self.driver.upclear('name', 'password1Post')
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123456')
        self.driver.send_keys('name', 'passwordPost', 'qf1')
        self.driver.send_keys('name', 'password1Post', 'qf1')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:red"]').text
        self.assertEqual('新密码不能少于4个字符', pw, msg='预期值与实际值不符')
        time.sleep(3)

    # 新旧密码相同
    def test_05(self):
        self.driver.upclear('name', 'passoldPost')
        self.driver.upclear('name', 'passwordPost')
        self.driver.upclear('name', 'password1Post')
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123456')
        self.driver.send_keys('name', 'passwordPost', 'qf123456')
        self.driver.send_keys('name', 'password1Post', 'qf123456')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:red"]').text
        self.assertEqual('新密码不能和旧密码相同', pw, msg='预期值与实际值不符')
        time.sleep(3)

    # 密码带有空格元素
    def test_06(self):
        self.driver.upclear('name', 'passoldPost')
        self.driver.upclear('name', 'passwordPost')
        self.driver.upclear('name', 'password1Post')
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123456')
        self.driver.send_keys('name', 'passwordPost', 'qf123 ')
        self.driver.send_keys('name', 'password1Post', 'qf123 ')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:green"]').text
        self.assertEqual('密码修改成功', pw, msg='预期值与实际值不符')
        time.sleep(3)
        self.driver.click('xpath', '//*[@id="indexuserl"]/a/div')
        time.sleep(3)
        self.driver.click('css', 'li[temp="4"]')
        time.sleep(3)
        self.driver.click('id', 'confirm_btn1')

    # 修改密码
    def test_07(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', 'qf123 ')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        self.driver.click('xpath', '//*[@id="menulist"]/a[1]')
        self.driver.click('xpath', '//*[@id="menu_list_grcog"]')
        time.sleep(3)
        self.driver.click('css', 'li[click="tesgs,1"]')
        self.driver.send_keys('name', 'passoldPost', 'qf123 ')
        self.driver.send_keys('name', 'passwordPost', 'qf123456')
        self.driver.send_keys('name', 'password1Post', 'qf123456')
        self.driver.click('name', 'submitbtn')
        pw = self.driver.getElement('css', 'span[style="color:green"]').text
        self.assertEqual('密码修改成功', pw, msg='预期值与实际值不符')
        time.sleep(3)


if __name__ == '__main__':
    unittest.main
